/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.service;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.momarketing.dao.CouponConfigDao;
import com.boxin.ims.modules.momarketing.entity.CouponConfig;
import com.boxin.ims.modules.wechat.score.entity.ScoreExchange;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 会员积分Service
 * @author Jakemanse
 * @version 2013-09-09
 */
@Component
@Transactional(readOnly = true)
public class CouponConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CouponConfigService.class);
	
	@Autowired
	private CouponConfigDao couponConfigDao;
	
	public CouponConfig get(Long id) {
		return couponConfigDao.findOne(id);
	}
	
	public Page<CouponConfig> find(Page<CouponConfig> page, CouponConfig couponConfig) {
		DetachedCriteria dc = couponConfigDao.createDetachedCriteria();
		dc.createAlias("user", "user");
		if (couponConfig.getUser()!=null &&couponConfig.getUser().getId()!=null&& couponConfig.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", couponConfig.getUser().getId()));
		}
		if (couponConfig.getUser()!=null &&StringUtils.isNotEmpty(couponConfig.getUser().getName())){
			dc.add(Restrictions.like("user.name", "%"+couponConfig.getUser().getName()+"%"));
		}
		if (StringUtils.isNotEmpty(couponConfig.getName())){
			dc.add(Restrictions.like("name", "%"+couponConfig.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(couponConfig.getType())){
			dc.add(Restrictions.eq("type", couponConfig.getType()));
		}
		if (StringUtils.isNotEmpty(couponConfig.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+couponConfig.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", CouponConfig.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return couponConfigDao.find(page, dc);
	}
	
	
	public Page<CouponConfig> findByMember(Page<CouponConfig> page, CouponConfig couponConfig) {
		DetachedCriteria dc = couponConfigDao.createDetachedCriteria();
		dc.createAlias("user", "user");
		if (couponConfig.getUser()!=null &&couponConfig.getUser().getId()!=null&& couponConfig.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", couponConfig.getUser().getId()));
		}
		if (couponConfig.getUser()!=null &&StringUtils.isNotEmpty(couponConfig.getUser().getName())){
			dc.add(Restrictions.like("user.name", "%"+couponConfig.getUser().getName()+"%"));
		}
		if (StringUtils.isNotEmpty(couponConfig.getName())){
			dc.add(Restrictions.like("name", "%"+couponConfig.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(couponConfig.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+couponConfig.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", CouponConfig.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return couponConfigDao.find(page, dc);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-11  下午3:57:12
	 * @function <p> 根据用户ID获取优惠券配置列表 </p>
	 * @param userId
	 * @return
	 */
	public List<CouponConfig> getCouponConfigListByUserId(Long userId){
		DetachedCriteria dc = couponConfigDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		List<CouponConfig> confs = couponConfigDao.find(dc);
		return confs;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-9  下午3:05:59
	 * @function <p> 根据 所属用户查询 优惠券配置 </p>
	 * @param userId
	 * @param keywords
	 * @return
	 */
	public CouponConfig getByUserIdAndKeywords(Long userId,String keywords){
		DetachedCriteria dc = couponConfigDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("keywords", keywords));
		List<CouponConfig> confs = couponConfigDao.find(dc);
		if(confs!=null){
			return (confs.size()==1) ? confs.get(0) : null;
		}
		return null;
	}
	
	public Long getCount(Long userId ){
		DetachedCriteria dc = couponConfigDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("type", "1"));
		return couponConfigDao.count(dc);
		
	}
	
	
	@Transactional(readOnly = false)
	public void save(CouponConfig couponConfig) {
		couponConfig.setRemarks(StringEscapeUtils.unescapeHtml4(couponConfig.getRemarks()) );
		couponConfigDao.save(couponConfig);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		couponConfigDao.deleteById(id);
	}
	
	
	
	public List<CouponConfig> findUserList( ){
		return couponConfigDao.find("from CouponConfig where type=1 group by user.id ", null);
		
	}
	
	
}
